/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { Module } from "vuex";
import { RootState } from "../utils/store.type";

const commandStatus : Module<any, RootState> = {
	namespaced: true,
	state: () => ({
		commandQuene: [],
		errorMsg: "",
		latestMsg: "",
		missionUploading: false,
		commandCodeList: []
	}),
	actions: {
		pushCommandCode(context, pyaloadCode) {
			context.commit("PUSHCOMMANDCODE", pyaloadCode);
		},
		popCommandCode(context, message) {
			if (message.code === "1001") {
				context.commit("POPCOMMANDCODE", message.pCode);
			}
		},
		deleteCommandCode(context) {
			console.log('出发没')
			context.commit("DELETECOMMANDCODE");
		}
	},
	mutations: {
		updateMissionUploading(state, payload) {
			state.missionUploading = payload;
		},
		PUSHCOMMANDCODE(state, pyaloadCode) {
			state.commandCodeList.push(pyaloadCode);
		},
		POPCOMMANDCODE(state, pyaloadCode) {
			state.commandCodeList = state.commandCodeList.filter(
				(item : string) => item.toString() !== pyaloadCode.toString()
			);
		},
		DELETECOMMANDCODE(state) {
			state.commandCodeList.splice(0);
		},
		pushCommand(state, payload) {
			// console.log(state,payload);
			// const json = JSON.parse(payload);
			state.commandQuene.push(payload);
		},
		popCommand(state, payload) {
			// for( let item of state.commandQuene){
			for (let i = 0; i < (state.commandQuene as Array<any>).length; i++) {
				const item = (state.commandQuene as Array<any>)[i];
				console.log(item.code, payload);
				if (item.code === payload) {
					state.commandQuene.splice(i, 1);
				}
			}
		},
		updateErrMsg(state, payload) {
			state.errorMsg = "";
			setTimeout(() => {
				state.errorMsg = payload;
			}, 500);
		}
	}
};

export default commandStatus;